import { ModalForm, ProFormSelect, ProFormTextArea } from '@ant-design/pro-components';
import { message } from 'antd';
import { updateAbnormalAppeal } from '../service';
import type { AbnormalAppealTableItem, UpdateAbnormalAppeal } from '../types.d';
import { AbnormalStatus } from '../types.d';

const HandleAppealModal = (props: {
  close: () => void;
  visible: boolean;
  reload: () => void;
  appeal: AbnormalAppealTableItem;
}) => {
  const { visible, close, appeal } = props;
  return (
    <ModalForm<UpdateAbnormalAppeal>
      title="申诉处理"
      width="45vw"
      layout="horizontal"
      modalProps={{
        destroyOnClose: true,
        onCancel: () => {
          close();
        },
      }}
      visible={visible}
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      initialValues={{
        status: appeal.status,
        result: appeal.result || '',
      }}
      onFinish={async (values) => {
        const { status } = values;
        const result = values?.result?.trim() || '';
        if (!result) {
          message.error({
            content: '处理结果不可为空!',
            key: 'handle_appeal',
          });
          return false;
        }
        const res = await updateAbnormalAppeal({
          no: appeal.no,
          status,
          result,
        });
        if (res?.success) {
          message.success({ key: 'handle_appeal', content: res.message });
          props.reload();
          return true;
        }
        message.error({ key: 'handle_appeal', content: res.message });
        return false;
      }}
    >
      <ProFormSelect
        label="申诉状态"
        name="status"
        disabled={appeal.status != AbnormalStatus.WAITING}
        required
        options={[
          {
            label: '待处理',
            value: 1,
            disabled: true,
          },
          {
            label: '已处理',
            value: 2,
          },
          {
            label: '已关闭',
            value: 3,
          },
        ]}
      />
      <ProFormTextArea required label="处理结果" name="result" />
    </ModalForm>
  );
};

export default HandleAppealModal;
